package com.xinchao.controller;

import com.xinchao.constant.AdvertisementConstant;
import com.xinchao.constant.ResponseConstant;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.Advertisement;
import com.xinchao.model.User;
import com.xinchao.model.dto.AdvertisementListDTO;
import com.xinchao.model.page.PageInfo;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.service.AdvertisementService;
import com.xinchao.util.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 广告Controller
 *
 * @author dxy
 * @date 2018/9/19 9:52
 */
@Controller
@RequestMapping(value = "/advertisement")
public class AdvertisementController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(AdvertisementController.class);
	@Autowired
	private AdvertisementService advertisementService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list")
	public String listAdvertisement(String advertiserName, String advertisementName, Integer advertisementStatus, Integer sex, String age,
	                                Integer pageNum, Integer pageSize, Model model) {
		//列表
		PageInfo<AdvertisementListDTO> pageInfo = advertisementService.listAdvertisement(advertiserName, advertisementName, advertisementStatus,
				sex, age, pageNum, pageSize);
		model.addAttribute("advertiserName", advertiserName);
		model.addAttribute("advertisementName", advertisementName);
		model.addAttribute("advertisementStatus", advertisementStatus);
		model.addAttribute("sex", sex);
		model.addAttribute("age", age);
		model.addAttribute("advertisementList", pageInfo.getList());
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		model.addAttribute("total", pageInfo.getTotal());
		//广告状态
		model.addAttribute("advertisementStatusMap", AdvertisementConstant.ADVERTISEMENT_STATUS_MAP);
		//性别
		model.addAttribute("sexMap", AdvertisementConstant.SEX_MAP);
		//年龄
		model.addAttribute("ageRangeList", AdvertisementConstant.AGE_RANGE_LIST);
		return "advertisement_list";
	}

	/**
	 * 添加
	 */
	@ResponseBody
	@RequestMapping(value = "/save")
	public Object saveAdvertisement(Advertisement advertisement, HttpServletRequest request) {
		//验证参数
		JsonResponse jsonResponse = advertisementService.validateAdvertisement(advertisement);
		if (jsonResponse != null) {
			return jsonResponse;
		}
		try {
			//登录用户
			User loginUser = getSessionUser(request);
			//远程登录ip
			String remortIP = HttpUtils.getRemortIP(request);
			//验证广告名称是否重复
			Advertisement oldAdvertisement = advertisementService.getAdvertisementByAdvertisementName(advertisement.getAdvertisementName());
			if (oldAdvertisement != null) {
				return new JsonResponse(ResponseConstant.CODE_FAIL, "广告名称已存在，请重新输入");
			}
			//保存广告并放入缓存
			advertisementService.saveAdvertisementFlushCache(advertisement, loginUser, remortIP);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("AdvertisementController addAdvertisement 添加广告主失败：" + e.getMessage());
			return e;
		}
		return "添加广告主成功";
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping(value = "/update")
	public Object updateAdvertisement(Advertisement advertisement, HttpServletRequest request) {
		//验证参数
		JsonResponse jsonResponse = advertisementService.validateAdvertisement(advertisement);
		if (jsonResponse != null) {
			return jsonResponse;
		}
		try {
			//验证广告名称是否重复
			Advertisement oldAdvertisement = advertisementService.getAdvertisementByAdvertisementName(advertisement.getAdvertisementName());
			if (oldAdvertisement != null) {
				if (!oldAdvertisement.getAdvertisementId().equals(advertisement.getAdvertisementId())) {
					return new JsonResponse(ResponseConstant.CODE_FAIL, "广告名称已存在，请重新输入");
				}
			}
			//登录用户
			User loginUser = getSessionUser(request);
			//远程登录ip
			String remortIP = HttpUtils.getRemortIP(request);
			//修改广告并放入缓存
			advertisementService.updateAdvertisementFlushCache(advertisement, loginUser, remortIP);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("AdvertisementController updateAdvertisement 修改广告主失败：" + e.getMessage());
			return e;
		}
		return "修改广告主成功";
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public Object deleteAdvertisement(Long advertisementId, HttpServletRequest request) {
		if (advertisementId == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告主ID不能为空，请联系管理员");
		}
		//登录用户
		User loginUser = getSessionUser(request);
		//远程登录ip
		String remortIP = HttpUtils.getRemortIP(request);
		try {
			//删除广告并从缓存中移除
			advertisementService.deleteAdvertisementFlushCache(advertisementId, loginUser, remortIP);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("AdvertisementController deleteAdvertisement 删除广告主失败：" + e.getMessage());
			return e;
		}
		return "删除成功";
	}

	/**
	 * 更新广告状态
	 */
	@ResponseBody
	@RequestMapping(value = "/updateAdvertisementStatus")
	public Object updateAdvertisementStatus(Long advertisementId, String advertisementName, Integer advertisementStatus, HttpServletRequest request) {
		//广告ID
		if (advertisementId == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告ID不能为空，请联系管理员");
		}
		//广告状态
		if (advertisementStatus == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告状态不能为空，请联系管理员");
		}
		//登录用户
		User loginUser = getSessionUser(request);
		//远程登录ip
		String remortIP = HttpUtils.getRemortIP(request);
		try {
			advertisementService.updateAdvertisementStatusFlushCache(advertisementId, advertisementName, advertisementStatus, loginUser, remortIP);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("AdvertisementController updateAdvertisementStatus 更新广告状态失败：" + e.getMessage());
			return e.getMessage();
		}
		return "";
	}


}
